package com.zhehekeji.opsys.api.service.validate;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhehekeji.opsys.api.code.LuckyAnswerResultCode;
import com.zhehekeji.opsys.api.common.util.SuperResp;
import com.zhehekeji.opsys.api.entity.TSurvey;
import com.zhehekeji.opsys.api.req.LuckyAnswerReq;
import com.zhehekeji.opsys.api.service.validate.args.LuckyAnswerValidateArgs;

/**
 * @ClassName LuckyAnswerValidate
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2016-11-11   下午4:45:06
 */
@Service
public class LuckyAnswerValidate extends ValidateService<ValidateRets<TSurvey>, LuckyAnswerValidateArgs>{

	private final static Logger logger = LoggerFactory.getLogger(LuckyAnswerValidate.class);
	
	@Override
	public ValidateRets<TSurvey> validate(LuckyAnswerValidateArgs validateArgs)
			throws Exception {
		ValidateRets<TSurvey> validateRets = new ValidateRets<TSurvey>();
		LuckyAnswerReq luckyAnswerReq = validateArgs.getLuckyAnswerReq();
		SuperResp resp = validateArgs.getResp();
		Date currentDate = validateArgs.getCurrentDate();
		
		if (luckyAnswerReq == null){
			logger.error("提交答案失败，参数非法！");
			resp.setResultCode(LuckyAnswerResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("提交答案失败，参数非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		if (StringUtils.isBlank(luckyAnswerReq.getUserID())){
			logger.error("提交答案失败，账号为空，参数非法！");
			resp.setResultCode(LuckyAnswerResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("提交答案失败，账号为空，参数非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		List<LuckyAnswerReq.AnswerInfo> answerInfos = luckyAnswerReq.getAnswerInfos();
		if (answerInfos == null || answerInfos.isEmpty()){
			logger.error("提交答案失败，答案为空，参数非法！");
			resp.setResultCode(LuckyAnswerResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("提交答案失败，答案为空，参数非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		for (LuckyAnswerReq.AnswerInfo tmpAnswerInfo : answerInfos){
			if (StringUtils.isBlank(tmpAnswerInfo.getChoiceIds())){
				logger.error("提交答案失败，选项为空，参数非法！");
				resp.setResultCode(LuckyAnswerResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("提交答案失败，选项为空，参数非法！");
				validateRets.setSuccess(false);
				return validateRets;
			}
			if (StringUtils.isBlank(tmpAnswerInfo.getChoiceOrdernos())){
				logger.error("提交答案失败，选项标识为空，参数非法！");
				resp.setResultCode(LuckyAnswerResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("提交答案失败，选项标识为空，参数非法！");
				validateRets.setSuccess(false);
				return validateRets;
			}
		}
		TSurvey tmpTSurvey =  entityDao.get(TSurvey.class, "o.code=?1", new Object[]{luckyAnswerReq.getSurveyCode()});
		
		if (tmpTSurvey == null){
			logger.error("提交答案失败，问卷不存在，参数非法！");
			resp.setResultCode(LuckyAnswerResultCode.SURVEY_DOES_NOT_EXIST);
			resp.setResultDesc("提交答案失败，问卷不存在，参数非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		if(tmpTSurvey.getStatus() == 0){
			logger.error("提交答案失败，问卷已停用，参数非法！");
			resp.setResultCode(LuckyAnswerResultCode.SURVEY_HAS_BEEN_STOPPED);
			resp.setResultDesc("提交答案失败，问卷已停用，参数非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		Date tmpStartTime = tmpTSurvey.getStartTime();
		Date tmpEndTime = tmpTSurvey.getEndTime();
		if (tmpStartTime == null || tmpEndTime == null){
			logger.error("提交答案失败，数据异常！");
			resp.setResultCode(LuckyAnswerResultCode.DATABASE_EXCEPTION);
			resp.setResultDesc("提交答案失败，数据异常！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		if (currentDate.compareTo(tmpStartTime) <0){
			logger.error("问卷还未开始！");
			resp.setResultCode(LuckyAnswerResultCode.SURVEY_HAS_YET_TO_BEGIN);
			resp.setResultDesc("问卷还未开始！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		if (currentDate.compareTo(tmpEndTime) >0){
			logger.error("问卷已经结束！");
			resp.setResultCode(LuckyAnswerResultCode.SURVEY_HAS_COME_TO_AN_END);
			resp.setResultDesc("问卷已经结束！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		validateRets.setEntity(tmpTSurvey);
		return validateRets;
	}

}

